/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bai1;

import java.io.File;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author ngadtt
 */
public class Domexample {

    static Document doc;
    static int count = 0;

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("heeeeeeee");
            System.exit(1);
        }
        try {
            File f = new File("tomcat-users.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc =(Document) db.parse(f);
            getUsers((Node) doc);
            System.out.println("no of users:" + count);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void getUsers(Node node) {
        if (node == null) {
            return;
        }
        if (node.getNodeName().equals("user")) {
            count++;
        }
        NodeList children = node.getChildNodes();
        int i = 0;
        while (i < children.getLength()) {
            getUsers((Node) children.item(i++));

        }
    }
}
